package sqlist;

import java.lang.reflect.Array;
import java.util.Arrays;

public class MyArrayList {
    public int[] ele;
    public int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.ele = new int[DEFAULT_SIZE];
    }

    //打印顺序表
    public void display(){
        for (int i = 0; i < usedSize; i++) {
            System.out.print(ele[i]+" ");
        }
        System.out.println();
    }

    //新增元素，默认在数组的最后新增
    public void add(int data){
        //1.判断是否满了，满了扩容
        if(isFull()){
            this.ele = Arrays.copyOf(this.ele,2*this.ele.length);
        }
        //2.没满的话直接添加
        this.ele[this.usedSize] = data;
        this.usedSize++;
    }
    private boolean isFull(){
        return this.ele.length == usedSize;
    }

    //在指定位置插入元素
    public void add(int data,int pos){
        //必须判断pos的合法性
        if (!checkPosInAdd(pos)){
            throw new MyArrayListIndexOutOfException("添加方法的pos不合理");
        }
        //1.判断有没有满
        if (isFull()){
            this.ele = Arrays.copyOf(this.ele,2*this.ele.length);
        }
        //2.插入指定元素
        for (int i = usedSize; i > pos; i--) {
            this.ele[i] = this.ele[i-1];
        }
        this.ele[pos] = data;
        this.usedSize++;
    }
    private boolean checkPosInAdd(int pos){
        if (pos < 0 || pos >usedSize){
            System.out.println("pos位置不合法");
            return false;
        }
        return true;
    }

    //判断是否包含某个元素
    public boolean contains(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.ele[i]){
                return true;
            }
        }
        return false;
    }

    //获取pos位置的值
    public int getPos(int pos){
        //不用写数组空不空
        if (this.usedSize == 0){
            throw new MyArrayListEmptyException("数组为空");
        }
        if (!checkPosInGet(pos)){
            throw new MyArrayListIndexOutOfException("pos位置不合法");
        }

        return this.ele[pos];
    }
    private boolean checkPosInGet(int pos){
        if (pos < 0 || pos >=usedSize){
            System.out.println("pos位置不合法");
            return false;
        }
        return true;
    }

    //更新方法
    public void set(int pos,int value){
        if(!checkPosInGet(pos)){
            throw new MyArrayListIndexOutOfException("pos位置不合法");
        }
        this.ele[pos] = value;
    }

    //删除方法
    public void del(int pos){
        if(this.usedSize == 0){
            throw new MyArrayListEmptyException("顺序表为空，不能删除");
        }
        if(!checkPosInGet(pos)){
            throw new MyArrayListIndexOutOfException("pos位置不合法");
        }
        for (int i = pos; i < this.usedSize-1; i++) {
            this.ele[i] = this.ele[i+1];
        }
        this.usedSize--;
        //如果i位置是引用类型，i位置要置空
    }
    //删除某个值
    public void remove(int value){
        if(this.usedSize == 0){
            throw new MyArrayListEmptyException("数组为空，不能删除");
        }
        int pos = getPosition(value);
        if(!checkPosInGet(pos)){
            throw new MyArrayListIndexOutOfException("pos位置不合法");
        }
        this.del(pos);
    }
    private int getPosition(int value){
        for (int i = 0; i < this.usedSize; i++) {
            if(value == this.ele[i]){
                return i;
            }
        }
        return -1;
    }

    //清空数组
    public void clear(){
//        for (int i = 0; i < this.usedSize; i++) {
//            this.ele[i] = null;
//        }
        this.usedSize = 0;
        //引用数组，得置null
    }

}
